
<template>
  <div>
    <el-dialog
      title="编辑接警人"
      :visible.sync="dialogVisible"
      width="500px"
      :before-close="closeDialog"
      :close-on-click-modal="false"
      :append-to-body="true"
    >
      <el-form
        :model="editForm"
        :rules="rules"
        ref="refForm"
        label-width="80px"
      >
        <el-form-item size="small" label="姓名" prop="name">
          <el-input
            clearable
            placeholder="请输入"
            maxlength="20"
            v-model.trim="editForm.name"
          >
          </el-input>
        </el-form-item>
        <el-form-item size="small" label="联系电话" prop="mobile">
          <el-input
            clearable
            placeholder="请输入"
            maxlength="12"
            v-model.trim="editForm.mobile"
          >
          </el-input>
        </el-form-item>
        <el-form-item size="small" label="通知方式" prop="notifyType">
          <el-checkbox-group v-model="editForm.notifyType">
            <el-checkbox label="语音" text-color="#ff00ff"></el-checkbox>
            <el-checkbox label="短信"></el-checkbox>
            <el-checkbox label="公众号"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog" class="cancel-button">取 消</el-button>
        <el-button
          size="small"
          @click="submit"
          v-preventReClick="1000" class="confirm-button"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { reqAlarmReceiverEdit } from "@/api/api";
import rulesMixin from "../mixins/rules.js";
import { mapState } from "vuex";
export default {
  props: {
    editData: {
      type: Object,
      default: () => {},
    },
  },
  mixins: [rulesMixin],
  data() {
    return {
      dialogVisible: false,
      editForm: {
        id: "",
        orgId: "",
        name: "",
        mobile: "",
        notifyType: [],
      },
    };
  },
  computed: { ...mapState(["user"]) },
  watch: {
    editData(val) {
      this.editForm.id = val.id;
      this.editForm.orgId = val.orgId;
      this.editForm.name = val.name;
      this.editForm.mobile = val.mobile;
      this.editForm.notifyType = [
        val.mobileReceive ? "语音" : "",
        val.mpReceive ? "公众号" : "",
        val.msgReceive ? "短信" : "",
      ];
    },
  },
  methods: {
    // 点击确定
    submit() {
      this.$refs.refForm.validate(async (valid) => {
        if (!valid) return;
        let { orgId, id, name, mobile, notifyType } = this.editForm;
        let mobileReceive = notifyType.indexOf("语音") !== -1 ? 1 : 0;
        let msgReceive = notifyType.indexOf("短信") !== -1 ? 1 : 0;
        let mpReceive = notifyType.indexOf("公众号") !== -1 ? 1 : 0;
        let params = {
          orgId,
          id,
          name,
          mobile,
          mobileReceive,
          msgReceive,
          mpReceive,
        };
        this.reqAddFun(params);
      });
    },
    // 接口请求
    async reqAddFun(params) {
      const res = await reqAlarmReceiverEdit(params);
      if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
      this.$modal.notifySuccess("编辑成功");
      this.$refs.refForm.resetFields();
      this.dialogVisible = false;
      this.$emit("clearEditData");
      this.$parent.getDataList();
    },
    // 点击关闭弹框
    closeDialog() {
      this.$refs.refForm.resetFields();
      this.dialogVisible = false;
      this.$emit("clearEditData");
    },
  },
  deactivated() {
    if(this.$refs.refForm!=null){
      this.closeDialog();
    }
  },
};
</script>

<style lang="less" scoped>
@import url("../../common.less");

::v-deep .el-checkbox__input.is-checked .el-checkbox__inner,
::v-deep .el-checkbox__input.is-indeterminate .el-checkbox__inner {
  background-color: #7367f0 !important;
  border: 1px solid #7367f0 !important;
}
::v-deep .el-checkbox__inner:hover {
  border-color: #7367f0;
}
::v-deep .el-checkbox__input.is-checked + .el-checkbox__label {
  color: #7367f0;
}
::v-deep .el-checkbox__input.is-focus .el-checkbox__inner {
  border-color: #7367f0 !important;
}
</style>